package com.smdk.dsminio.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


public class DateUtil {
    /**
     * 相差天数
     *
     * @param str1
     * @param str2
     * @return
     * @throws Exception
     */
    public static Integer getDistanceDays(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date one;
        Date two;
        long days = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            days = diff / (1000 * 60 * 60 * 24);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return Integer.parseInt(String.valueOf(days));
    }


    /**
     * 相差天数
     *
     * @param one
     * @param two
     * @return
     * @throws Exception
     */
    public static Integer getDistanceDays(Date one, Date two) {
        long days = 0;
        try {
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            days = diff / (1000 * 60 * 60 * 24);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Integer.parseInt(String.valueOf(days));
    }

    /**
     * 相差多少秒
     *
     * @param data1 时间参数 1 格式：1990-01-01 12:00:00
     * @param date2 时间参数 2 格式：2009-01-01 12:00:00
     *              秒
     */
    public static long getDistanceTimes(Date data1, Date date2) {
        Date one;
        Date two;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        long sec2 = 0;
        try {
            one = data1;
            two = date2;
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            day = diff / (24 * 60 * 60 * 1000);
            hour = (diff / (60 * 60 * 1000) - day * 24);
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
            sec2 = (hour * 60 * 60) + (min * 60) + sec;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sec2;
    }

    /**
     * 两个时间相差距离多少天多少小时多少分多少秒
     *
     * @param str1 时间参数 1 格式：1990-01-01 12:00:00
     * @param str2 时间参数 2 格式：2009-01-01 12:00:00
     * @return String 返回值为：xx天xx小时xx分xx秒
     */
    public static String getDistanceTime(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date one;
        Date two;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            day = diff / (24 * 60 * 60 * 1000);
            hour = (diff / (60 * 60 * 1000) - day * 24);
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day + "天" + hour + "小时" + min + "分" + sec + "秒";
    }

    /**
     * 格式化日期 字符串转日期
     *
     * @param str1
     * @return
     * @throws Exception
     */
    public static Date FromDistanceDays(String str1) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date data = null;

        try {
            data = df.parse(str1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return data;
    }

    /**
     * 日期转字符串
     *
     * @param data1
     * @return
     * @throws Exception
     */
    public static String FromDistanceDays(Date data1) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String data = null;

        try {
            data = df.format(data1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }



    /**
     * 日期转字符串
     *
     * @param date
     * @return
     * @throws Exception
     */
    public static String FromDistanceDaysNumber(Date date) {
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String data = null;
        try {
            data = df.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

    /**
     * 当前日期天数加一
     *
     * @param str1
     * @return
     * @throws Exception
     */
    public static String DataAdd(String str1, int size) {
        Calendar c = Calendar.getInstance();
        c.setTime(DateUtil.FromDistanceDays(str1));   //设置当前日期
        c.add(Calendar.DATE, size);
        Date date = c.getTime(); //结果  
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String addData = df.format(date);
        return addData;
    }

    /**
     * 计算当前日期到下月的一号还有多少秒
     *
     * @return
     */

    public static int getSecond() {
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        Date date = new Date();
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        int a = ca.get(Calendar.DAY_OF_MONTH);
        int second = (day - a) * 60 * 60 * 24;
        return second;
    }

    /**
     * 获取当然日期离下个礼拜还1还有多少 秒
     */
    public static long getSecondWeek() throws ParseException {
        int a = 0;
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(System.currentTimeMillis()));
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        switch (dayOfWeek) {
            case 1:
                a = 7;
                break;
            case 2:
                a = 1;
                break;
            case 3:
                a = 2;
                break;
            case 4:
                a = 3;
                break;
            case 5:
                a = 4;
                break;
            case 6:
                a = 5;
                break;
            case 7:
                a = 6;
                break;

        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String b = df.format(new Date()) + " 23:59:59";
        long d = DateUtil.getDistanceTimes(new Date(), df1.parse(b));
        d = d + (7 - a) * 24 * 60 * 60;
        return d;

    }


    /**
     * 获取今天是星期几
     */
    public static int getWeek() throws ParseException {
        int a = 0;
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(System.currentTimeMillis()));
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        switch (dayOfWeek) {
            case 1:
                a = 7;
                break;
            case 2:
                a = 1;
                break;
            case 3:
                a = 2;
                break;
            case 4:
                a = 3;
                break;
            case 5:
                a = 4;
                break;
            case 6:
                a = 5;
                break;
            case 7:
                a = 6;
                break;

        }

        return a;

    }


    /**
     * 获取今天是星期几
     */
    public static String getWeekToPinYin() throws ParseException {
        String a = "";
        Calendar c = Calendar.getInstance();
        c.setTime(new Date(System.currentTimeMillis()));
        int dayOfWeek = c.get(Calendar.DAY_OF_WEEK);
        switch (dayOfWeek) {
            case 1:
                //周日
                a = "Sunday_7";
                break;
            case 2:
                a = "Monday_1";
                break;
            case 3:
                a = "Tuesday_2";
                break;
            case 4:
                a = "Wednesday_3";
                break;
            case 5:
                a = "Thursday_4";
                break;
            case 6:
                a = "Friday_5";
                break;
            case 7:
                a = "Saturday_6";
                break;

        }

        return a;

    }

    /***
     *  功能描述：日期转换cron表达式
     * @param date
     * @param dateFormat : e.g:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String formatDateByPattern(Date date, String dateFormat) {
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        String formatTimeStr = null;
        if (date != null) {
            formatTimeStr = sdf.format(date);
        }
        return formatTimeStr;
    }

    /***
     * convert Date to cron ,eg.  "0 07 10 15 1 ? 2016"
     * @param date  : 时间点
     * @return
     */
    public static String getCron(Date date) {
        String dateFormat = "ss mm HH dd MM ? yyyy";
        return formatDateByPattern(date, dateFormat);
    }


    /**
     * 判断选择的日期本月的第几天
     */
    public static int getNowDate() {
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.DATE);
    }

    /**
     * 获取当前时间之前或之前几小时 hour
     *
     * @param hour
     * @return
     */
    public static Date getTimeReduceHour(Date date, int hour) {
        Calendar c = Calendar.getInstance();
        c.setTime(date); //设置时间
        c.add(Calendar.HOUR, hour); //日期分钟加1,Calendar.DATE(天),Calendar.HOUR(小时)
        return c.getTime();
    }

    /**
     * 获取当前时间之前或之后几小时 hour
     *
     * @param hour
     * @return
     */
    public static String getTimeAddHour(int hour) {

        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) + hour);

        return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());

    }

    /**
     * 获得当前月的前N个月
     *
     * @param month
     * @return
     */
    public static String getLastMonth(int month) {
        LocalDate today = LocalDate.now();
        today = today.minusMonths(month);
        DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM");
        return formatters.format(today);
    }

    /**
     * 使用Java8的 ZonedDateTime 时间新特性判断 月份是否相同
     *
     * @param d1
     * @param d2
     * @return
     */
    public static boolean equalsMonth(Date d1, Date d2) {
        LocalDate localDate1 = ZonedDateTime.ofInstant(d1.toInstant(), ZoneId.systemDefault()).toLocalDate();
        LocalDate localDate2 = ZonedDateTime.ofInstant(d2.toInstant(), ZoneId.systemDefault()).toLocalDate();
        return localDate1.getMonth().equals(localDate2.getMonth());
    }

    /**
     * 拍判断是否为同一天
     *
     * @param d1
     * @param d2
     * @return
     */
    public static boolean isTheSameDay(Date d1, Date d2) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(d1);
        c2.setTime(d2);
        return (c1.get(Calendar.YEAR) == c2.get(Calendar.YEAR))
                && (c1.get(Calendar.MONTH) == c2.get(Calendar.MONTH))
                && (c1.get(Calendar.DAY_OF_MONTH) == c2.get(Calendar.DAY_OF_MONTH));
    }
}  
